package org.service.impl;

import java.util.List;

import org.bean.O2rankrank;
import org.bean.O2rankscore;
import org.bean.O2rankscorecount;
import org.dao.ScoreDao;
import org.service.ScoreService;
import org.util.PageBean;

public class ScoreServiceImpl implements ScoreService {

	private ScoreDao scoredao;
	private O2rankscore O2rankscore;

	@SuppressWarnings("unchecked")
	@Override
	public PageBean queryForPageScore(int pageSize, int page) {
		final String hql;

		hql = "from O2rankscore s order by s.o2ranksongs.mid desc";
		int allRow = scoredao.getAllRowCount(hql);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int length = pageSize;
		final int currentPage = PageBean.countCurrentPage(page);

		List<O2rankrank> list = scoredao.queryForPageScore(hql, offset, length);

		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();

		return pageBean;
	}

	@Override
	public PageBean queryForPageScoreBYAsc(int pageSize, int page) {
		final String hql;

		hql = "from O2rankscore s order by s.o2ranksongs.hardlevel";
		int allRow = scoredao.getAllRowCount(hql);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int length = pageSize;
		final int currentPage = PageBean.countCurrentPage(page);

		List<O2rankrank> list = scoredao.queryForPageScore(hql, offset, length);

		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();

		return pageBean;
	}

	@Override
	public PageBean queryForPageScoreBYDesc(int pageSize, int page) {
		final String hql;

		hql = "from O2rankscore s order by s.o2ranksongs.hardlevel desc";
		int allRow = scoredao.getAllRowCount(hql);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int length = pageSize;
		final int currentPage = PageBean.countCurrentPage(page);

		List<O2rankrank> list = scoredao.queryForPageScore(hql, offset, length);

		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();

		return pageBean;
	}

	@Override
	public PageBean queryForPageScoreCount(int pageSize,int page) {
		final String hql;

		hql = "from O2rankscorecount c order by c.count desc";

		int allRow = scoredao.getAllRowCount(hql);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int length = pageSize;
		final int currentPage = PageBean.countCurrentPage(page);

		List<O2rankrank> list = scoredao.queryForPageScore(hql, offset, length);

		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();

		return pageBean;
	}

	public ScoreDao getScoredao() {
		return scoredao;
	}

	public void setScoredao(ScoreDao scoredao) {
		this.scoredao = scoredao;
	}

	public O2rankscore getO2rankscore() {
		return O2rankscore;
	}

	public void setO2rankscore(O2rankscore o2rankscore) {
		O2rankscore = o2rankscore;
	}

}
